home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / stevie.arc / ALLOC.C next >
Text File  |  1990-01-10  |  4KB  |  200 lines

  1. /*
  2.  * STevie - ST editor for VI enthusiasts.    ...Tim Thompson...twitch!tjt...
  3.  *
  4.  * Extensive modifications by:  Tony Andrews       onecom!wldrdg!tony
  5.  * Turbo C 1.5 port by: Denny Muscatelli 061988
  6.  */
  7.  
  8. #include "stevie.h"
  9.  
  10. /*
  11.  * This file contains various routines dealing with allocation and
  12.  * deallocation of data structures.
  13.  */
  14.  
  15. char *
  16. alloc(size)
  17. unsigned size;
  18. {
  19.     char *p;        /* pointer to new storage space */
  20.  
  21.     p = malloc(size);
  22.     if ( p == (char *)NULL ) {    /* if there is no more room... */
  23.         emsg("alloc() is unable to find memory!");
  24.     }
  25.     return(p);
  26. }
  27.  
  28. char *
  29. strsave(string)
  30. char *string;
  31. {
  32.     return(strcpy(alloc((unsigned)(strlen(string)+1)),string));
  33. }
  34.  
  35. void
  36. screenalloc()
  37. {
  38.     /*
  39.      * If we're changing the size of the screen, free the old arrays
  40.      */
  41.     if (Realscreen != NULL)
  42.         free(Realscreen);
  43.     if (Nextscreen != NULL)
  44.         free(Nextscreen);
  45.  
  46.     Realscreen = malloc((unsigned)(Rows*Columns));
  47.     Nextscreen = malloc((unsigned)(Rows*Columns));
  48. }
  49.  
  50. /*
  51.  * Allocate and initialize a new line structure with room for
  52.  * 'nchars' characters.
  53.  */
  54. LINE *
  55. newline(nchars)
  56. int    nchars;
  57. {
  58.     register LINE    *l;
  59.  
  60.     if ((l = (LINE *) alloc(sizeof(LINE))) == NULL)
  61.         return (LINE *) NULL;
  62.  
  63.     l->s = alloc(nchars);        /* the line is empty */
  64.     l->s[0] = NUL;
  65.     l->size = nchars;
  66.  
  67.     l->prev = (LINE *) NULL;    /* should be initialized by caller */
  68.     l->next = (LINE *) NULL;
  69.  
  70.     return l;
  71. }
  72.  
  73. /*
  74.  * filealloc() - construct an initial empty file buffer
  75.  */
  76. void
  77. filealloc()
  78. {
  79.     if ((Filemem->linep = newline(1)) == NULL) {
  80.         fprintf(stderr,"Unable to allocate file memory!\n");
  81.         exit(1);
  82.     }
  83.     if ((Fileend->linep = newline(1)) == NULL) {
  84.         fprintf(stderr,"Unable to allocate file memory!\n");
  85.         exit(1);
  86.     }
  87.     Filemem->index = 0;
  88.     Fileend->index = 0;
  89.  
  90.     Filemem->linep->next = Fileend->linep;
  91.     Fileend->linep->prev = Filemem->linep;
  92.  
  93.     *Curschar = *Filemem;
  94.     *Topchar  = *Filemem;
  95.  
  96.     Filemem->linep->num = 0;
  97.     Fileend->linep->num = 0xffff;
  98.  
  99.     clrall();        /* clear all marks */
  100. }
  101.  
  102. /*
  103.  * freeall() - free the current buffer
  104.  *
  105.  * Free all lines in the current buffer.
  106.  */
  107. void
  108. freeall()
  109. {
  110.     LINE    *lp, *xlp;
  111.  
  112.     for (lp = Filemem->linep; lp != NULL ;lp = xlp) {
  113.         if (lp->s != NULL)
  114.             free(lp->s);
  115.         xlp = lp->next;
  116.         free(lp);
  117.     }
  118.  
  119.     Curschar->linep = NULL;        /* clear pointers */
  120.     Filemem->linep = NULL;
  121.     Fileend->linep = NULL;
  122. }
  123.  
  124. /*
  125.  * bufempty() - return TRUE if the buffer is empty
  126.  */
  127. bool_t
  128. bufempty()
  129. {
  130.     return (buf1line() && Filemem->linep->s[0] == NUL);
  131. }
  132.  
  133. /*
  134.  * buf1line() - return TRUE if there is only one line
  135.  */
  136. bool_t
  137. buf1line()
  138. {
  139.     return (Filemem->linep->next == Fileend->linep);
  140. }
  141.  
  142. /*
  143.  * lineempty() - return TRUE if the current line is empty
  144.  */
  145. bool_t
  146. lineempty()
  147. {
  148.     return (Curschar->linep->s[0] == NUL);
  149. }
  150.  
  151. /*
  152.  * endofline() - return TRUE if the given position is at end of line
  153.  *
  154.  * This routine will probably never be called with a position resting
  155.  * on the NUL byte, but handle it correctly in case it happens.
  156.  */
  157. bool_t
  158. endofline(p)
  159. register LPTR    *p;
  160. {
  161.     return (p->linep->s[p->index] == NUL || p->linep->s[p->index+1] == NUL);
  162. }
  163. /*
  164.  * canincrease(n) - returns TRUE if the current line can be increased 'n' bytes
  165.  *
  166.  * This routine returns immediately if the requested space is available.
  167.  * If not, it attempts to allocate the space and adjust the data structures
  168.  * accordingly. If everything fails it returns FALSE.
  169.  */
  170. bool_t
  171. canincrease(n)
  172. register int    n;
  173. {
  174.     register int    nsize;
  175.     register char    *s;        /* pointer to new space */
  176.  
  177.     nsize = strlen(Curschar->linep->s) + 1 + n;    /* size required */
  178.  
  179.     if (nsize <= Curschar->linep->size)
  180.         return TRUE;
  181.  
  182.     /*
  183.      * Need to allocate more space for the string. Allow some extra
  184.      * space on the assumption that we may need it soon. This avoids
  185.      * excessive numbers of calls to malloc while entering new text.
  186.      */
  187.     if ((s = alloc(nsize + SLOP)) == NULL) {
  188.         emsg("Can't add anything, file is too big!");
  189.         State = NORMAL;
  190.         return FALSE;
  191.     }
  192.  
  193.     Curschar->linep->size = nsize + SLOP;
  194.     strcpy(s, Curschar->linep->s);
  195.     free(Curschar->linep->s);
  196.     Curschar->linep->s = s;
  197.     
  198.     return TRUE;
  199. }
  200.